package dao.impl;

import dao.AdminDao;
import entity.Admin;
import util.DBUtil;

import java.util.List;

public class AdminDaoIpml implements AdminDao {
    @Override
    public int addAdmin(Admin admin) {
        String sql = "insert into admin(username,password,name,address,birthday) values(?,?,?,?,?)";
        return DBUtil.executeUpdate(sql,admin.getUsername(),admin.getPassword(),admin.getName(),admin.getAddress(),admin.getBirthday());
    }

    @Override
    public int updAdmin(Admin admin) {
        String sql = "update admin set username=?,password=?,name=?,address=?,birthday=? where id=?";
        return DBUtil.executeUpdate(sql,admin.getUsername(),admin.getPassword(),admin.getName(),admin.getAddress(),admin.getBirthday(),admin.getId());
    }

    @Override
    public int delAdminById(Integer id) {
        String sql = "delete from admin where id=?";
        return DBUtil.executeUpdate(sql,id);
    }

    @Override
    public List<Admin> findAllAdmin() {
        String sql = "select * from admin";
        List<Admin> list = DBUtil.executeQuery(Admin.class,sql);
        return list;
    }

    @Override
    public Admin findAdminById(Integer id) {
        String sql = "select * from admin where id = ?";
        List<Admin> list = DBUtil.executeQuery(Admin.class,sql,id);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public boolean isExistById(Integer id) {
        Admin admin = this.findAdminById(id);
        return admin == null ? false : true;
    }

    @Override
    public Admin login(String name, String pwd) {
        return null;
    }
}
